//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "PdeShortRate.h"
using namespace Cephei::QL::Methods::Finitedifferences;
#include <gen/QL/Math/TransformedGrid.h>
#include <gen/QL/Methods/Finitedifferences/TridiagonalOperator.h>
#include <gen/QL/Methods/Finitedifferences/PdeSecondOrderParabolic.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::CPdeShortRate::CPdeShortRate (boost::shared_ptr<QuantLib::PdeShortRate>& childNative, Object^ owner) : CPdeSecondOrderParabolic(CPdeShortRate::typeid)
{
#ifdef HANDLE
	_phPdeShortRate = NULL;
#endif
	_ppPdeShortRate = &childNative;
    _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeShortRate));
}
Cephei::QL::Methods::Finitedifferences::CPdeShortRate::CPdeShortRate (QuantLib::PdeShortRate& childNative, Object^ owner) : CPdeSecondOrderParabolic(CPdeShortRate::typeid)
{
#ifdef HANDLE
	_phPdeShortRate = NULL;
#endif
	_ppPdeShortRate = new boost::shared_ptr<QuantLib::PdeShortRate> (&childNative);
    _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeShortRate));
    _PdeShortRateOwner = owner;
    _PdeSecondOrderParabolicOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::CPdeShortRate::CPdeShortRate (CPdeShortRate^ copy) : CPdeSecondOrderParabolic(CPdeShortRate::typeid)
{
#ifdef HANDLE
	_phPdeShortRate = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPdeShortRate = new boost::shared_ptr<QuantLib::PdeShortRate> (copy->GetShared());
        _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeShortRate));
    }
}
Cephei::QL::Methods::Finitedifferences::CPdeShortRate::CPdeShortRate (PLATFORM::Type^ t) : CPdeSecondOrderParabolic(CPdeShortRate::typeid)
{
#ifdef HANDLE
	_phPdeShortRate = NULL;
#endif
	if (!t->IsSubclassOf(CPdeShortRate::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::CPdeShortRate::CPdeShortRate (QuantLib::Handle<QuantLib::PdeShortRate>& childNative, Object^ owner)  : CPdeSecondOrderParabolic(CPdeShortRate::typeid)
{
	_phPdeShortRate = &childNative;
	_ppPdeShortRate = &static_cast<boost::shared_ptr<QuantLib::PdeShortRate>>(childNative.currentLink());
    _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeShortRate));
    _PdeShortRateOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::CPdeShortRate::CPdeShortRate (QuantLib::Handle<QuantLib::PdeShortRate> childNative)  : CPdeSecondOrderParabolic(CPdeShortRate::typeid)
{
	_phPdeShortRate = &childNative;
	_ppPdeShortRate = &static_cast<boost::shared_ptr<QuantLib::PdeShortRate>>(childNative.currentLink());
    _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeShortRate));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::CPdeShortRate::CPdeShortRate (QuantLib::PdeShortRate childNative)  : CPdeSecondOrderParabolic(CPdeShortRate::typeid)
{
#ifdef HANDLE
	_phPdeShortRate = NULL;
#endif
	_ppPdeShortRate = new boost::shared_ptr<QuantLib::PdeShortRate> (new QuantLib::PdeShortRate (childNative));
    _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeShortRate));
}
#endif

Cephei::QL::Methods::Finitedifferences::CPdeShortRate::~CPdeShortRate ()
{
    if (_ppPdeShortRate != NULL)
    {
	    delete _ppPdeShortRate;
        _ppPdeShortRate = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::CPdeShortRate::!CPdeShortRate ()
{
    if (_ppPdeShortRate != NULL)
    {
	    delete _ppPdeShortRate;
    }
}
QuantLib::PdeShortRate& Cephei::QL::Methods::Finitedifferences::CPdeShortRate::GetReference ()
{
    if (_ppPdeShortRate == NULL) throw REFNEW NativeNullException ();
	return **_ppPdeShortRate;
}
boost::shared_ptr<QuantLib::PdeShortRate>& Cephei::QL::Methods::Finitedifferences::CPdeShortRate::GetShared ()
{
    if (_ppPdeShortRate == NULL) throw REFNEW NativeNullException ();
	return *_ppPdeShortRate;
}
QuantLib::PdeShortRate* Cephei::QL::Methods::Finitedifferences::CPdeShortRate::GetPointer ()
{
    if (_ppPdeShortRate == NULL) throw REFNEW NativeNullException ();
	return &**_ppPdeShortRate;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PdeShortRate>& Cephei::QL::Methods::Finitedifferences::CPdeShortRate::GetHandle ()
{
	if (_phPdeShortRate == NULL)
	{
		_phPdeShortRate = new Handle<QuantLib::PdeShortRate> (*_ppPdeShortRate);
	}
	return *_phPdeShortRate;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::CPdeShortRate::HasNative () 
{
	return (_ppPdeShortRate != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::CPdeShortRate::Diffusion (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppPdeShortRate)->diffusion ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::CPdeShortRate::Discount (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppPdeShortRate)->discount ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::CPdeShortRate::Drift (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppPdeShortRate)->drift ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

